/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 文件名：	QtReportProvider.java
 * 模块说明：	
 * 修改历史：
 * Jun 9, 2007 - zhangyanbo - 创建。
 */
package com.hd123.rumba.qt;

import java.io.File;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

import net.sf.hibernate.HibernateException;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.qt.def.IQueryDef;
import com.hd123.rumba.qt.def.IQueryDefinitionMgr;
import com.hd123.rumba.qt.def.QueryDefinition;
import com.hd123.rumba.qt.def.SQLOrder;
import com.hd123.rumba.qt.result.IQueryResultMgr;
import com.hd123.rumba.qt.result.QueryResultDescription;
import com.hd123.rumba.report.IReportProvider;
import com.hd123.rumba.request.Dictionary;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.sql.SQLSubquery;
import com.hd123.rumba.util.MidasUtil;

/**
 * @author zhangyanbo
 * 
 */
public class QtReportProvider implements IReportProvider {

  public String getId() throws Exception {
    return "qtreport";
  }

  public File getReport(Dictionary dd) throws Exception {
    IQueryResultMgr qrm = (IQueryResultMgr) NamingMgr.getInstance()
        .lookupMgr(IQueryResultMgr.class);
    IQueryDefinitionMgr qdm = (IQueryDefinitionMgr) NamingMgr.getInstance().lookupMgr(
        IQueryDefinitionMgr.class);
    String qrdUuid = (String) dd.getFormValue("qrdUuid");
    QueryResultDescription qrd = (QueryResultDescription) qrm.findQrdByUuid(qrdUuid);
    if (qrd == null)
      throw new BusinessException("找不到指定的查询结果(uuid=" + qrdUuid + ")。");
    IQueryDef queryDef = qdm.getQueryDef(qrd.getQdUuid());
    if (queryDef == null)
      throw new BusinessException("找不到指定的查询定义(uuid=" + qrd.getQdUuid() + ")。");
    return queryDef.getTemplateFile();
  }

  public void getData(Dictionary dd, OutputStream os) throws Exception {
    IQueryResultMgr qrm = (IQueryResultMgr) NamingMgr.getInstance()
        .lookupMgr(IQueryResultMgr.class);
    IQueryDefinitionMgr qdm = (IQueryDefinitionMgr) NamingMgr.getInstance().lookupMgr(
        IQueryDefinitionMgr.class);
    String qrdUuid = (String) dd.getFormValue("qrdUuid");
    if (qrdUuid == null || qrdUuid.length() == 0)
      throw new Exception("指定的参数qrdUuid不能为空。");
    QueryResultDescription qrd = (QueryResultDescription) qrm.findQrdByUuid(qrdUuid);
    if (qrd == null)
      throw new Exception("找不到指定的查询结果(qrdUuid=" + qrdUuid + ")。");
    QueryDefinition qd = qdm.getQueryDefinition(qrd);
    if (qd == null)
      throw new Exception("找不到对应的查询定义。");
    SQLSubquery sq = new SQLSubquery();
    sq.addSelect("*");
    sq.addFrom(qrd.getTableName());
    for (int i = 0; i < qrd.getDisplayCriteria().getOrders().size(); ++i) {
      SQLOrder o = (SQLOrder) qrd.getDisplayCriteria().getOrders().get(i);
      sq.addOrder(o.getColumn(), o.getOrder());
    }
    Connection conn = null;
    Statement stm = null;
    ResultSet rs = null;
    try {
      try {
        conn = QueryResultSession.getSession().connection();
      } catch (HibernateException e) {
        throw new MaxQueryException(e);
      }
      stm = conn.createStatement();
      rs = stm.executeQuery(sq.getSqlStatement());
      List titles = qd.getColumns().getVisibleColumnTitles();
      os.write("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>".getBytes());
      MidasUtil.resultSet2DataPacket(rs, os, titles);
    } finally {
      if (rs != null)
        rs.close();
      if (stm != null)
        stm.close();
    }
  }
}
